package root.electronclass.electronclass.other

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo
import root.electronclass.electronclass.common.event.NetWorkEvent
import org.greenrobot.eventbus.EventBus


class NetworkChangeReceiver : BroadcastReceiver() {



    override fun onReceive(p0: Context?, p1: Intent?) {
        val connectivityManager: ConnectivityManager = p0?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        val networkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo

        if (networkInfo != null && networkInfo.isAvailable()) {
//            Toast.makeText(context, "当前网络可用", Toast.LENGTH_SHORT).show()
            EventBus.getDefault().post(NetWorkEvent(true))
        } else {
//            Toast.makeText(context, "当前网络不可用", Toast.LENGTH_SHORT).show()
            EventBus.getDefault().post(NetWorkEvent(false))
        }
    }
}